<?php
class ModelLotteryLottery extends Model {
	public function addLottery($data) {
		$this->event->trigger('pre.admin.lottery.add', $data);

		$this->db->query("INSERT INTO " . DB_PREFIX . "lottery SET name = '".$this->db->escape($data['lottery_name'])."', date_added = NOW()");

		$lottery_id = $this->db->getLastId();

		$this->cache->delete('lottery');

		$this->event->trigger('post.admin.lottery.add', $lottery_id);

		return $lottery_id;
	}

	public function editLottery($lottery_id, $data) {
	    $this->event->trigger('pre.admin.lottery.edit', $data);

	    $this->db->query("UPDATE " . DB_PREFIX . "lottery SET name = '" . $this->db->escape($data['lottery_name']) . "' WHERE lottery_id = '" . (int)$lottery_id . "'");

	    $this->cache->delete('lottery');

	    $this->event->trigger('post.admin.lottery.edit', $lottery_id);
	}


	public function deleteLottery($lottery_id) {
	    $this->event->trigger('pre.admin.lottery.delete', $lottery_id);

	    $this->db->query("DELETE FROM " . DB_PREFIX . "lottery WHERE lottery_id = '" . (int)$lottery_id . "'");
	    $this->db->query("DELETE FROM " . DB_PREFIX . "lottery_reward WHERE lottery_id = '" . (int)$lottery_id . "'");
	    $this->cache->delete('lottery');
	    $this->event->trigger('post.admin.lottery.delete', $lottery_id);
	}


	public function getLottery($lottery_id) {
	    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "lottery where lottery_id = '".(int)$lottery_id."' ");

	    return $query->row;
	}


	public function getLotteries($data = array()) {
	    $sql = "SELECT * FROM " . DB_PREFIX . "lottery order by date_added desc";

	    if (isset($data['start']) || isset($data['limit'])) {
	        if ($data['start'] < 0) {
	            $data['start'] = 0;
	        }

	        if ($data['limit'] < 1) {
	            $data['limit'] = 20;
	        }

	        $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
	    }

	    $query = $this->db->query($sql);

	    return $query->rows;
	}

	public function getAllLotteries(){
	    $sql = "SELECT * FROM " . DB_PREFIX . "lottery order by date_added desc";
	    $query = $this->db->query($sql);

	    return $query->rows;
	}

	public function getTotalLotteries() {
	    $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "lottery");

	    return $query->row['total'];
	}


	public function addLotteryReward($data){
	    $this->event->trigger('pre.admin.lottery_reward.add', $data);

	    $this->db->query("INSERT INTO " . DB_PREFIX . "lottery_reward SET name = '".$this->db->escape($data['name'])."',lottery_id = '".(int)$data['lottery_id']."',type = '".(int)$data['type']."',points = '".(int)$data['points']."',link='".$this->db->escape($data['link'])."',stock_num = '".(int)$data['stock_num']."', date_added = NOW()");

	    $lottery_reward_id = $this->db->getLastId();

	    $this->cache->delete('lottery_reward');

	    $this->event->trigger('post.admin.lottery_reward.add', $lottery_reward_id);

	    return $lottery_reward_id;
	}


	public function editLotteryReward($lottery_reward_id, $data) {


	    $this->event->trigger('pre.admin.lottery_reward.edit', $data);


	    $this->db->query("UPDATE " . DB_PREFIX . "lottery_reward SET  name = '".$this->db->escape($data['name'])."',lottery_id = '".(int)$data['lottery_id']."',type = '".(int)$data['type']."',points = '".(int)$data['points']."',link='".$this->db->escape($data['link'])."',stock_num = '".(int)(int)$data['stock_num']."' where lottery_reward_id = '".(int)$lottery_reward_id."'");

	    $this->cache->delete('lottery_reward');

	    $this->event->trigger('post.admin.lottery_reward.edit', $lottery_reward_id);
	}


	public function deleteLotteryReward($lottery_reward_id) {
	    $this->event->trigger('pre.admin.lottery_reward.delete', $lottery_reward_id);
	    $this->db->query("DELETE FROM " . DB_PREFIX . "lottery_reward WHERE lottery_reward_id = '" . (int)$lottery_reward_id . "'");
	    $this->cache->delete('lottery_reward');
	    $this->event->trigger('post.admin.lottery_reward.delete', $lottery_reward_id);
	}


	public function getLotteryReward($lottery_reward_id) {
	    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "lottery_reward where lottery_reward_id = '".(int)$lottery_reward_id."' ");

	    return $query->row;
	}


	public function getLotteryRewards($data = array()) {
	    $sql = "SELECT lr.*,l.name as lottery_name FROM " . DB_PREFIX . "lottery_reward as lr left join " . DB_PREFIX . "lottery as l on lr.lottery_id = l.lottery_id  ";

        if(isset($data['fliter_name'])){
            $sql .="where l.name like %'".$this->db->escape($data['fliter_name'])."'%";
        }

	    $sql .="order by date_added desc";

	    if (isset($data['start']) || isset($data['limit'])) {
	        if ($data['start'] < 0) {
	            $data['start'] = 0;
	        }

	        if ($data['limit'] < 1) {
	            $data['limit'] = 20;
	        }

	        $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
	    }

	    $query = $this->db->query($sql);

	    return $query->rows;
	}



	public function getTotalLotteryRewards($data = array()) {
	    $sql = "SELECT COUNT(*) as total  FROM " . DB_PREFIX . "lottery_reward as lr left join " . DB_PREFIX . "lottery as l on lr.lottery_id = l.lottery_id  ";

        if(isset($data['fliter_name'])){
            $sql .="where l.name like %'".$this->db->escape($data['fliter_name'])."'%";
        }

	    $sql .="order by lr.date_added desc";

	    $query = $this->db->query($sql);

	    return $query->row['total'];
	}

}
